package cn.itcast.crm.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.crm.dao.CustomerRepository;
import cn.itcast.crm.domain.Customer;
import cn.itcast.crm.service.CustomerService;
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
	//注入Dao中
	@Autowired
	private CustomerRepository customerRepository;
	//查询所有的没有关联的用户
	@Override
	public List<Customer> findNoAssocationCustomer() {
		
		return customerRepository.findByFixedAreaIdNull();
	}
	//查询已经关联的用户
	@Override
	public List<Customer> findHasAssocationFixedAreaCustomer(String fixedAreaId) {
		return customerRepository.findByFixedAreaId(fixedAreaId);
	}
	//将用户关联到指定区域
	@Override
	public void assocationCustomerToFixedArea(String CustomerIdStr, String fixedAreaId) {
		//传过来的是1，2，3的数据我们需要进行切分
		//解除关联动作
		customerRepository.cleanFixedAreaId(fixedAreaId);
		if(StringUtils.isBlank(CustomerIdStr)){
			return;
		}
		String[] customerIdStr = CustomerIdStr.split(",");
		for (String IdStr : customerIdStr) {
			int id = Integer.parseInt(IdStr);
			customerRepository.updateFixedAreaId(fixedAreaId, id);
		}
	}
	@Override
	public void regist(Customer customer) {
		customerRepository.save(customer);
	}
	
	@Override
	public Customer findByTelephone(String telephone) {
		
		return customerRepository.findByTelephone(telephone);
	}
	@Override
	public void updateType(String telephone) {
		customerRepository.updateType(telephone);
	}

}
